package datastruct.sort;

import java.util.Arrays;

/**
 * @program: DataStructuresAndAlgorithms
 * @description: 希尔排序
 * @author: zhang
 * @create: 2020-03-13 12:35
 */
public class ShellSortDemo {

    public static void shellSort(int[] arr) {
        int temp = 0;
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                //遍历各组中所有的元素（共组，每组有2各元素），步长5
                for (int j = i - gap; j >= 0; j -= gap) {
                    //如果当前元素大于加上步长后的那个元素，说明交换
                    if (arr[j] > arr[j + gap]) {
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 7, 3, 0, 4, 9, 2, 4, 22, 21, 20};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}